import Vue from 'vue'
import App from './App'
import store from './store';
import COMM from './common/common.js';
import TIM from 'tim-wx-sdk';
import COS from "cos-wx-sdk-v5";
import hkModal from "@/components/hk-modal/hk-modal.vue";
import emptyBox from "@/components/emptyBox/emptyBox.vue";
import ttBar from "@/components/ttBar/ttBar.vue";
import newlogin from "./components/newlogin.vue"

Vue.config.productionTip = false
Vue.prototype.$store = store;
Vue.component('newlogin',newlogin);
Vue.component('hkModal',hkModal);
Vue.component('emptyBox',emptyBox);
Vue.component('ttBar',ttBar);

/**
 * 创建TIM并挂载全局Vue
 */
let options = {
	SDKAppID: 1400287788 //  SDKAppID
};
const tim = TIM.create(options); // 创建TIM实例

//注册COS SDK 插件
tim.registerPlugin({
	'cos-wx-sdk': COS
});
tim.setLogLevel(1); //设置日志级别

// 监听收到消息,包括收到单聊/群聊/群提示/群系统通知.
// TODO  收到消息后创建本地推送
tim.on(TIM.EVENT.MESSAGE_RECEIVED, event => {
	//TODO 收到消息后添加switchTab标记,
	uni.$emit("timReceivedChange", event)
})
// 监听会话列表更新
// TODO 列表有变化是修改底部tabbar角标
tim.on(TIM.EVENT.CONVERSATION_LIST_UPDATED, event => {
	uni.$emit("timCovListChange", event.data)
})
// 监听事件SDK ready
tim.on(TIM.EVENT.SDK_READY, function(event) {
	getApp().globalData.TIM_SDK_READY=true;
});

Vue.prototype.$tim = tim; //将创建好的TIM实例挂载在全局Vue上



/** 页面跳转逻辑
 * @param {Object} target 点击页面元素后会传递被点击的target 
 */
Vue.prototype.toPage = function(e, onlyOne = false) {
	let params = {};
	if (typeof e == "string") { //只有url符串
		params.url = e
	} else if (e.hasOwnProperty('currentTarget')) { //触摸点击跳转
		params = e.currentTarget.dataset
		delete params.eventOpts //删除点击的eventOpts属性
	} else { //传递参数跳转
		params = e
	}
	console.log("页面参数:"); //传递参数
	console.log(params);

	// 打开外部网页
	if (params.url.indexOf("http") > -1) {
		uni.navigateTo({
			url: '../../pagesA/pageView/pageView?' + COMM.paramsURL(params)
		});
	} else { //打开内部页面
		if(params.url.indexOf("?")>-1){
			if(params.url.split('?')[1]){
				params=COMM.innerUrlToObj(params.url)
			}else{
				params.url=params.url.split('?')[0]
			}
		
		}
		if (params.url == "index" || params.url == "conversation" || params.url == "my" || params.url == "lmList"|| params.url == "shoprk" || params.url == "growthRecord") { //如果为switchTab页面
			uni.switchTab({
				url: '../../pages/' + params.url + '/' + params.url
			});
		} else { //非switchTab页面
			if (params.url) {
				let pageOpenInfo = COMM.checkPageOpendByName(params.url);
				// console.log('路由深度结构:');
				// console.log(pageOpenInfo);
				if (onlyOne) {
					if (pageOpenInfo.opendCount > 0) {
						uni.$emit('backUpdate', params); //注册更新事件并发送参数
						//页面回退
						uni.navigateBack({
							delta: pageOpenInfo.backFirstLength
						})
					} else {
						if(params.url=="login"){
							uni.navigateTo({
								url: "../../pages/" + params.url + "/" + params.url + "?" + COMM.paramsURL(params)
							})
						}else if(params.url=="helpPlan" || params.url=="help" || params.url=="myHelp" || params.url=="ffhelpPlan" || params.url=="zhelpPlan" || params.url=="chelpPlan" || params.url== "myoldnew" || params.url == "oldfuli" || params.url =="oldfulifriend" || params.url =="oldnewxiangxiangqing" || params.url == "mybaoming" || params.url == "cgxiangqing" || params.url == "orderDtCg"|| params.url == "shopsearch"){
							uni.navigateTo({
								url: "../../pagesB/" + params.url + "/" + params.url + "?" + COMM.paramsURL(params)
							})
						}else{
							uni.navigateTo({
								url: "../../pagesA/" + params.url + "/" + params.url + "?" + COMM.paramsURL(params)
							})
						}
						
					}
				} else {
					// 如果已页面栈已超过9个,并且要打开的目标页面已经打开过
					if (pageOpenInfo.pageCount > 9 && pageOpenInfo.opendCount > 0) { //如果已打开,则回退
						uni.$emit('backUpdate', params); //注册更新事件并发送参数
						//页面回退
						uni.navigateBack({
							delta: pageOpenInfo.backLastLength
						})
					} else {
						if(params.url=="login"){
							uni.navigateTo({
								url: "../../pages/" + params.url + "/" + params.url + "?" + COMM.paramsURL(params)
							})
						} else if(params.url=="helpPlan" || params.url=="help" || params.url=="myHelp" || params.url=="ffhelpPlan" || params.url=="zhelpPlan" || params.url=="chelpPlan" || params.url== "myoldnew" || params.url == "oldfuli" || params.url =="oldfulifriend" || params.url =="oldnewxiangxiangqing"|| params.url == "mybaoming"|| params.url == "cgxiangqing" || params.url == "orderDtCg" || params.url == "shopsearch"){
							uni.navigateTo({
								url: "../../pagesB/" + params.url + "/" + params.url + "?" + COMM.paramsURL(params)
							})
						}else{
							uni.navigateTo({
								url: "../../pagesA/" + params.url + "/" + params.url + "?" + COMM.paramsURL(params)
							})
						}
						
						
						
						
					}
				}
			}
		}
	}
}

/**
 * @description 页面回退
 * @param {Number} e 回退数量,默认为1,即关闭当前页,回退到上一个页面
 */
Vue.prototype.pBack = function(e) {
	// console.log(e);
	if(typeof e != 'number'){
		e=0
	}
	uni.navigateBack({
		delta: e ? e : 1
	})
}
// 字符串去头尾
String.prototype.tailCut = function() {
	return this.replace(/(^\s*)|(\s*$)/g, '')
}
/**
 * 对Date的扩展，将 Date 转化为指定格式的String
 * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符
 * 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
 * eg:
 * (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
 * (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04
 * (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04
 * (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04
 * (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
 */
Date.prototype.pattern = function(fmt) {
	var o = {
		"M+": this.getMonth() + 1, //月份
		"d+": this.getDate(), //日
		"h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
		"H+": this.getHours(), //小时
		"m+": this.getMinutes(), //分
		"s+": this.getSeconds(), //秒
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度
		"S": this.getMilliseconds() //毫秒
	};
	var week = {
		"0": "日",
		"1": "一",
		"2": "二",
		"3": "三",
		"4": "四",
		"5": "五",
		"6": "六"
	};
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	if (/(E+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "星期" : "周") : "") + week[this.getDay() +
			""]);
	}
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		}
	}
	return fmt;
}

App.mpType = 'app';

const app = new Vue({
	store,
	...App
})
app.$mount()
